package com.aaa.controller;

import com.aaa.entity.Dept;
import com.aaa.entity.MyReturn;
import com.aaa.entity.PageSort;
import com.aaa.service.DeptService;

import com.aaa.util.Enum01;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


@Controller
@RequestMapping("dept")
public class DeptController extends BaseController{
    @Autowired
    private DeptService deptService;

    MyReturn myReturn=new MyReturn();


    @RequestMapping("toShowDeptPage.do")
    public String toShowUserPage(){
        return "showDept";
    }

    /**
    * @Author: WTH
    * @Description: 部门查询,分页,计算总条数
    * @Date:2022-07-01 09:30
    * @Params:
    */
    @ResponseBody
    @RequestMapping("selectDept.do")
    public MyReturn selectDept(PageSort pageSort){
        Page<Dept> page = deptService.selectDept(pageSort);
        myReturn.setData(page.getResult());
        super.getSuccessReturn(myReturn);
        myReturn.setCount(page.getTotal());
        return myReturn;
    }


    /**
    * @Author: WTH
    * @Description: 部门新增
    * @Date:2022-07-01 09:30
    * @Params:
    */
    @ResponseBody
    @RequestMapping("addDept.do")
    public MyReturn addDept(Dept dept){
        dept.setCreate_time(new Date());
        dept.getCreate_by();
        Enum01 enum01 = deptService.addDept(dept);
        myReturn.setCode(enum01.getCode());
        myReturn.setMsg(enum01.getMsg());
        return myReturn;
    }

    /**
    * @Author: WTH
    * @Description: 单条删除，删除校验
    * @Date:2022-07-01 09:30
    * @Params:
    */
    @ResponseBody
    @RequestMapping("deleteDept.do")
    public MyReturn deleteDept(Integer dept_id){
        Enum01 enum01 = deptService.deleteDeptByid(dept_id);
        myReturn.setCode(enum01.getCode());
        myReturn.setMsg(enum01.getMsg());
        return myReturn;
    }

    /**
    * @Author: WTH
    * @Description: 部门修改
    * @Date:2022-07-01 09:30
    * @Params:
    */
    @ResponseBody
    @RequestMapping("updateDept.do")
    public MyReturn updateDept(Dept dept){
        dept.setUpdate_time(new Date());
        Enum01 i = deptService.updateDept(dept);
        myReturn.setCode(i.getCode());
        myReturn.setMsg(i.getMsg());
        return myReturn;
    }

    /**
    * @Author: WTH
    * @Description: 部门批量删除
    * @Date:2022-07-01 09:32
    * @Params:
    */
    @ResponseBody
    @RequestMapping("deleteAllDept.do")
    public MyReturn deleteDept(String dept_id){
        boolean b = deptService.deleteDept(dept_id);
        if (b){
            return super.getSuccessReturn(myReturn);
        }
        return super.getFailReturn(myReturn);
    }


    /**
    * @Author: WTH
    * @Description: 部门名称重复性校验
    * @Date:2022-07-08 15:21
    * @Params:  * @param null
    * @return:
    */
    @ResponseBody
    @RequestMapping("selectDeptName.do")
    public MyReturn selectDeptName(Dept dept_name){
        boolean b = deptService.selectDeptName(dept_name);
        if(b){
             super.getFailReturn(myReturn);
        }else{
             super.getSuccessReturn(myReturn);
        }
        return myReturn;
    }
}
